package flc.ast.adapter;

import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class MovieAdapter extends StkProviderMultiAdapter<StkResBean> {

    public MovieAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new MovieProvider());
    }

    private class MovieProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_recommend;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            Glide.with(getContext()).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivRecommendItemImg));
            baseViewHolder.setText(R.id.tvRecommendItemName, bean.getName());
            if (bean.isSelected()) {
                baseViewHolder.setImageResource(R.id.ivRecommendItemCollect, R.drawable.ysc1);
            } else {
                baseViewHolder.setImageResource(R.id.ivRecommendItemCollect, R.drawable.wsc1);
            }
            try {
                Gson gson = new Gson();
                String json = gson.toJson(bean.getExtra());
                JSONObject jsonObject = new JSONObject(json);
                String releaseDate = jsonObject.getString("releaseDate");
                int index = releaseDate.indexOf("(");
                String dateStr = releaseDate.substring(0, index);
                Date date = TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy-MM-dd"));
                if (date!=null){
                    baseViewHolder.setText(R.id.tvRecommendItemDate,getContext().getString(R.string.release_date)+TimeUtils.date2String(date, new SimpleDateFormat("yyyy年MM月dd日")));
                }else {
                    baseViewHolder.setText(R.id.tvRecommendItemDate,getContext().getString(R.string.release_date)+dateStr);
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
